package huang.jdbc.jan07;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import zhou.jan11.contest07.User;

public class UserDao {
	public static void main(String[] args) {
		UserDao adduser=new UserDao();
//		User u=new User();
//		u.setUsername("testname");
//		u.setPassword("555");
//		adduser.add1(u);
//		adduser.update(u);
//		User u1=new User();
//		u1.setUsername("xiaomi2");
//		u1.setPassword("159357");
//		UserDao ud1=new UserDao();
//		ud1.update(u1);
		User u2=new User();
		u2.setUsername("黄文衡");
		u2.setPassword("guodaxia");
		adduser.add2(u2);
	}
	//修改：
	public boolean update(User u){
		BaseDao bd=new BaseDao();
		Connection con=bd.getCon();
		String sql="update user set username=? where id=2";
		
		try {
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, u.getUsername());
			ps.execute();
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return true;
	}	
	public boolean add1(User u){
		BaseDao dao=new BaseDao();
		Connection con =dao.getCon();
		
		String sql="insert into user (username,pwd) values('"+u.getUsername()+"','"+u.getPassword()+"')";
		
		
				try {
					PreparedStatement ps =con.prepareStatement(sql);
					ps.executeUpdate();
					if(con!=null){
						con.close();
					}
					if(ps!=null){
						ps.close();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
				
				return true;
	}
	public boolean add2(User u){
		BaseDao dao=new BaseDao();
		Connection con =dao.getCon();
		
		String sql="insert into user (username,pwd) values(?,?)";
		
				try {
					PreparedStatement ps =con.prepareStatement(sql);
					ps.setString(1, u.getUsername());
					ps.setString(2, u.getPassword());
					ps.executeUpdate();
					if(con!=null){
						con.close();
					}
					if(ps!=null){
						ps.close();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
				
				return true;
	}
}
